﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace CalendarScreenSaver
{
    public class Settings
    {
        public string UserName { get; set; }
        public byte[] UserNameByteArray 
        {
            get { return Encoding.ASCII.GetBytes(UserName); } 
        }        
        public string CalendarName { get; set; }
        public byte[] CalendarNameByteArray
        {
            get { return Encoding.ASCII.GetBytes(CalendarName); }
        }        
    }

    public static class SettingsManager
    {
        public static void SaveSettings(Settings settings)
        {
            string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string filePath = System.IO.Path.Combine(path, "CalendarScreenSaver\\settings.dat");
/*
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048))
            {
                RSAParameters rsaParams = rsa.ExportParameters(false);
                rsa.ImportParameters(rsaParams);
                using (TextWriter tw = File.CreateText(filePath))
                {
                    string usernameEncrypted = Convert.ToBase64String(rsa.Encrypt(settings.UserNameByteArray, false));
                    string calendarEncrypted = Convert.ToBase64String(rsa.Encrypt(settings.CalendarNameByteArray, false));
                    tw.WriteLine(usernameEncrypted);
                    tw.WriteLine(calendarEncrypted);
                    tw.Flush();
                    tw.Close();
                }
            }
 * */
            using (TextWriter tw = File.CreateText(filePath))
            {
                tw.WriteLine(settings.UserName);
                tw.WriteLine(settings.CalendarName);
                tw.Flush();
                tw.Close();
            }
        }

        public static Settings ReadSettings()
        {
            string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string filePath = System.IO.Path.Combine(path, "CalendarScreenSaver\\settings.dat");
            Settings settings = null;

            if (!Directory.Exists(System.IO.Path.Combine(path, "CalendarScreenSaver")))
            {
                Directory.CreateDirectory(System.IO.Path.Combine(path, "CalendarScreenSaver"));
            }

            if (File.Exists(filePath))
            {
                try
                {
                    using (TextReader reader = new StreamReader(filePath))
                    {
                        string userName = reader.ReadLine();
                        string calendarName = reader.ReadLine();

                        settings = new Settings { UserName = userName, CalendarName = calendarName };

                        reader.Close();
                    }
                    /*
                    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048))
                    {
                        settings.UserName = Encoding.Unicode.GetString(rsa.Decrypt(settings.UserNameByteArray, false));
                        settings.CalendarName = Encoding.Unicode.GetString(rsa.Decrypt(settings.CalendarNameByteArray, false));
                    }
                     * */
                }
                catch (Exception ex)
                {
                    throw ex;
                    //return null;
                }

                if (string.IsNullOrEmpty(settings.UserName) || string.IsNullOrEmpty(settings.CalendarName))
                {
                    return null;
                }
            }

            return settings; // ?? new Settings { UserName = "caribfreeads@gmail.com", CalendarName = "Tasks" }
        }
    }
}
